//
//  zipTableViewController.m
//  gdemm
//
//  Created by 李园园 on 17/3/10.
//  Copyright © 2017年 Goldencis. All rights reserved.
//
#import "DocManager.h"
#import "zipTableViewController.h"
#import "DocManager.h"
#import "InboxManager.h"
#import "FileInfo.h"
#import "FileHandler.h"

@interface zipTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,copy)NSString * unzipPathTo;
@end

@implementation zipTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}



-(instancetype)initWithStyle:(UITableViewStyle)style{
    if ([super initWithStyle:style]) {
        self.tableView.backgroundColor = [Utils UIColorFromHex:0Xf5f6f8];
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
    }
    return self;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.fileArr.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseid = @"reuseId";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseid];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseid];
    }
    cell.backgroundColor = [Utils UIColorFromHex:0Xf5f6f8];
    FileInfo * info = self.fileArr[indexPath.row];
    NSString *fileTypeIconPath = [[DocManager getInstance]getPathMappedIconId:info.fileName];
    UIImage * img;
    if ([fileTypeIconPath containsString:@"cloudDisk_icon_unknown_enced"]||[fileTypeIconPath containsString:@"cloudDisk_icon_unknown"] ) {
        img = [UIImage imageNamed:@"cloudDisk_icon_file"];
    }
    else{
        img = [Utils imageWithKey:fileTypeIconPath];
    }
    
    cell.textLabel.text = info.fileName;
    //对image付值
    cell.imageView.image = img;
    //调整大小
    CGSize itemSize = CGSizeMake(25, 25);
    UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    FileInfo *fileInfo =   self.fileArr[indexPath.row];
    if (![[DocManager getInstance]isDocumentSupported:fileInfo.fileName]) {
        NSString *suffix = [StringUtil getSuffix:fileInfo.fileName];
        [self.view makeToast:[NSString stringWithFormat:NSLocalizedString(@"str_notSupportTheFormatFile:%@", @"暂不支持查看该格式文件"),suffix]];
        return;
    }
    if ([[NSFileManager defaultManager]fileExistsAtPath:fileInfo.localPath]) {
         UIViewController *showVc = [[DocManager getInstance]getControllerForFilePath:fileInfo.localPath];
        [self.navigationController pushViewController:showVc animated:YES];
    }
    
}

@end
